#include "input.hpp"

#include <GLFW/glfw3.h>

namespace core::input {
    InputContext::InputContext() {
        memset(key_state, 0x00, sizeof(key_state));
        memset(mouse_button_state, 0x00, sizeof(mouse_button_state));
        Reset();
    }

    InputContext::~InputContext() { window = nullptr; }

    void InputContext::Init(void *win) { this->window = win; }

    b8 InputContext::IsKeyPressed(key_code key) const
    {
        if (window) {
            const int state = glfwGetKey(static_cast<GLFWwindow *>(window),
                                         static_cast<int>(key));
            return GLFW_PRESS == state;
        }
        return false;
    }

    b8 InputContext::IsMousePressed(mouse_button button) const
    {
        if (window) {
            const int state = glfwGetMouseButton(
                static_cast<GLFWwindow *>(window), static_cast<int>(button));
            return GLFW_PRESS == state;
        }
        return false;
    }

    glm::vec2 InputContext::GetMousePos() const
    {
        if (window) {
            double x, y;
            glfwGetCursorPos(static_cast<GLFWwindow *>(window), &x, &y);
            return {static_cast<float>(x), static_cast<float>(y)};
        }
        return {};
    }

    void InputContext::Reset() {
        resized = false;
        wheel = {};
    }
}  // namespace core::input
